home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadcom / acrx / sample / fact.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  8.0 KB  |  338 lines

  1. /* Next available MSG number is   8 */
  2.  
  3. /***************************************************************************
  4.    Module Name:  fact.cc
  5.  
  6.    Copyright (C) 1994 by Autodesk, Inc.
  7.  
  8.    Permission to use, copy, modify, and distribute this software in 
  9.    object code form for any purpose and without fee is hereby granted, 
  10.    provided that the above copyright notice appears in all copies and 
  11.    that both that copyright notice and the limited warranty and 
  12.    restricted rights notice below appear in all supporting 
  13.    documentation.
  14.  
  15.    AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.  
  16.    AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF 
  17.    MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
  18.    DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE 
  19.    UNINTERRUPTED OR ERROR FREE.
  20.  
  21.    Use, duplication, or disclosure by the U.S. Government is subject to 
  22.    restrictions set forth in FAR 52.227-19 (Commercial Computer 
  23.    Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) 
  24.    (Rights in Technical Data and Computer Software), as applicable.
  25.     
  26.    .
  27.  
  28.    Description:  Another Sample ADS application
  29.  
  30.    Author     : 
  31.                  Autodesk, Inc.
  32.                  2320 Marinship Way
  33.                  Sausalito, CA. 94965
  34.                  (415)332-2344
  35.  
  36.     This Arx application is a conversion from the original sample ADS app
  37.     fact.c.
  38.  
  39.     CREATED BY:  Anita Gottapati & Ed Becnel  January 15, 1994
  40.  
  41.     Function Entry Points: 
  42.       AcRx::AppRetCode
  43.         acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt);
  44.  
  45.     Exported ADS Functions
  46.         SQRT
  47.     FACT
  48.  
  49.     Modification History:
  50.         Jan 31 1994 - eab - modified to use ads_defun()
  51.  
  52.     Notes and restrictions on use:
  53.  
  54.  
  55. ***************************************************************************/
  56.  
  57.  
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include "rxdefs.h"
  61. #include "adslib.h"
  62.  
  63.  
  64. extern int squareRoot();
  65. extern ads_real rsqr(ads_real );
  66.  
  67. extern int fact();
  68. static ads_real rfact(int );
  69.  
  70. int funcload   (void);
  71. int funcunload (void);
  72. int dofun      (void);
  73.  
  74. /* ADS Function Table */
  75. typedef struct {
  76.     char    *name;
  77.     int     (*fptr)();
  78. } ftblent;
  79.  
  80. #define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
  81.  
  82. /* Table of ADS functions */
  83. ftblent exfun[] = {
  84.             {/*MSG0*/"SQRT", squareRoot},
  85.         {/*MSG0*/"FACT", fact}
  86.         };
  87.  
  88. extern "C" 
  89. {                         
  90. AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg,void * pkt);
  91. }
  92.  
  93.  
  94. /******************************************************************************/
  95. /*.doc funcload(internal) */
  96. /*+
  97.     This function is called to define all function names in the ADS
  98.     function table.  Each named function will be callable from lisp or
  99.     invokable from another ADS application.
  100. -*/
  101. /******************************************************************************/
  102. int
  103. /*FCN*/funcload()
  104. {
  105.     int i;
  106.  
  107.     for (i = 0; i < ELEMENTS(exfun); i++) {
  108.         if (!ads_defun(exfun[i].name, i))
  109.             return RTERROR;
  110.     }
  111.  
  112.     return RTNORM;
  113. }
  114.  
  115. /******************************************************************************/
  116. /*.doc funclunoad(internal) */
  117. /*+
  118.     This function is called to undefine all function names in the ADS
  119.     function table.  Each named function will be removed from the
  120.     AutoLISP hash table.
  121. -*/
  122. /******************************************************************************/
  123. int
  124. /*FCN*/funcunload()
  125. {
  126.     int i;
  127.  
  128.     /* Undefine each function we defined */
  129.  
  130.     for (i = 0; i < ELEMENTS(exfun); i++) {
  131.         ads_undef(exfun[i].name,i);
  132.     }
  133.  
  134.     return RTNORM;
  135. }
  136.  
  137. /******************************************************************************/
  138. /*.doc dofun(internal) */
  139. /*+
  140.     This function is called to invoke the function which has the
  141.     registerd function code that is obtained from  ads_getfuncode.  The
  142.     function will return RTERROR if the function code is invalid, or
  143.     RSERR if the invoked function fails to return RTNORM.  The value
  144.     RSRSLT will be returned if the function code is valid and the
  145.     invoked subroutine returns RTNORM.
  146. -*/
  147. /******************************************************************************/
  148. int
  149. /*FCN*/dofun()
  150. {
  151.     int    val;
  152.     int    rc;
  153.  
  154.     ads_retvoid();
  155.         
  156.     if ((val = ads_getfuncode()) < 0 || val > ELEMENTS(exfun))
  157.         return RTERROR;
  158.  
  159.     rc = (*exfun[val].fptr)();
  160.  
  161.     return ((rc == RTNORM) ? RSRSLT:RSERR);
  162. }
  163.  
  164.  
  165. AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg, void* ptr)
  166. {
  167.  
  168.     if (ptr != NULL) {
  169.         // We have been handed some kind of object
  170.         // but we aren't going to do anything with it.
  171.     }
  172.  
  173.     switch(msg) {
  174.     case AcRx::kInitAppMsg:
  175.         break;
  176.         case AcRx::kInvkSubrMsg:
  177.             dofun();
  178.             break;
  179.         case AcRx::kLoadADSMsg:
  180.             funcload();
  181.             break;
  182.         case AcRx::kUnloadADSMsg:
  183.             funcunload();
  184.             ads_printf(/*MSG2*/"Unloading.\n");
  185.             break;
  186.     case AcRx::kUnloadAppMsg:
  187.         default:
  188.         break;
  189.     }
  190.     return AcRx::kRetOK;
  191. }
  192.  
  193.  
  194. int squareRoot()
  195. {
  196.     ads_real val;
  197.     ads_real retval = -1.0;
  198.     int i = RTNORM;
  199.     int getinput = FALSE;
  200.     struct resbuf *arg = NULL;
  201.     
  202.     arg = ads_getargs();
  203.     if (arg) {
  204.         switch(arg->restype) {
  205.     case RTREAL:
  206.             val = arg->resval.rreal;
  207.         break;
  208.     case RTSHORT:
  209.             val = (ads_real)arg->resval.rint;
  210.         break;
  211.     default:
  212.         getinput = TRUE;
  213.     }
  214.     } else {
  215.         getinput = TRUE;
  216.     }
  217.  
  218.     if (getinput)
  219.         i = ads_getreal("Enter a positive real/integer number: ",&val);
  220.     
  221.     switch (i) {
  222.     case RTNORM:
  223.         if (val < 0.0) { 
  224.             ads_fail("Square root of negative number is not defined\n");
  225.             break;
  226.         }
  227.     retval = rsqr(val);
  228.         break;
  229.  
  230.     case RTNONE:
  231.         ads_printf("Null input to prompt !! \n");
  232.         break;
  233.  
  234.     case RTCAN:
  235.         ads_printf("Command Cancelled \n");
  236.         break;
  237.  
  238.     case RTERROR:
  239.         ads_printf("Error returned from ads_getreal \n");
  240.     return RTERROR;
  241.     default:
  242.         ads_printf("Unknown return code from ads_getreal \n");
  243.     return RTERROR;
  244.     }
  245.     if (retval == -1.0)
  246.         ads_retnil();
  247.     else
  248.         ads_retreal(retval);
  249.     return RTNORM;
  250. }
  251.  
  252.  
  253. ads_real rsqr(ads_real val)
  254. {
  255.     int n = 50;
  256.         
  257.     if (val == 0.0)
  258.         return 0.0;
  259.         
  260.     ads_real y = (val * 2 + 0.1) / (val + 0.1);
  261.     ads_real c = (y - val /y) /2;
  262.     ads_real c1 = 0.0;
  263.         
  264.     while ((c != c1) && (n-- >0)) { 
  265.         y -= c;
  266.         c1 = c;
  267.         c = (y - val/y) /2;
  268.     }
  269.     return y;
  270. }
  271.         
  272.         
  273. int fact()
  274. {
  275.     // prompt for the input value
  276.     int val,rcode = RTNORM;
  277.     ads_real retval = -1.0;
  278.     int getinput = FALSE;
  279.     struct resbuf *arg = NULL;
  280.     
  281.     arg = ads_getargs();
  282.     if (arg) {
  283.         switch(arg->restype) {
  284.     case RTREAL:
  285.             val = (int)arg->resval.rreal;
  286.         break;
  287.     case RTSHORT:
  288.             val = arg->resval.rint;
  289.         break;
  290.     default:
  291.         getinput = TRUE;
  292.     }
  293.     } else {
  294.         getinput = TRUE;
  295.     }
  296.  
  297.     if (getinput)
  298.         rcode = ads_getint("Input positive integer value less than 170 \n", &val);
  299.         
  300.     switch (rcode) { 
  301.     case RTNORM:
  302.         if ((val  > 170) || ( val < 0)) {
  303.             ads_fail("Illegal Value \n");
  304.         break;
  305.         }
  306.         retval = rfact(val);
  307.         break;
  308.     case RTNONE:
  309.         ads_printf("Null input to prompt !! \n");
  310.         break;
  311.     case RTCAN:
  312.         ads_printf("Command Cancelled \n");
  313.         break;
  314.     case RTERROR:
  315.         ads_printf("Error returned from ads_getint \n");
  316.     return RTERROR;
  317.     default:
  318.         ads_printf("Unknown Error Code \n");
  319.     return RTERROR;
  320.     }
  321.     if (retval == -1.0)
  322.         ads_retnil();
  323.     else
  324.         ads_retreal(retval);
  325.     return RTNORM;
  326. }
  327.  
  328. static ads_real rfact(int n)
  329. {
  330.     ads_real ans = 1.0;
  331.  
  332.     while (n)
  333.         ans *= (ads_real)n--;
  334.  
  335.     return ans;
  336. }
  337.  
  338.